// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// See License.txt in the project root for license information.
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Actors;
using System.Threading.Actors.Utilities;
using System.Threading.Tasks;

namespace TestActorMethods
{
    [TestClass]
    public class TcpClientTests
    {
        // Simple test to ensure that we properly instantiate an ITcpClient from
        // ActorRawTcpClient.CreateTcpClientFunc.  I can manually turn on and off the
        // WrappedTcpClient static constructor, to make sure that 
        // ActorRawTcpClient.DefaultCreateTcpClient also works correctly.
        [TestMethod]
        public void TcpClient_Test()
        {
            // The first time through, we should hit the default reflection-based creator, which should
            // result in a port number of 2.
            ITcpClient client = LightupITcpClientFactory.CreateTcpClientFunc(null, -1); // Special non-connecting constructor
            Assert.IsNotNull(client, "Expected ARTC.CreateTcpClientFunc call 1 to return non-null value");
            Type t = Type.GetType("System.Threading.Actors.Utilities.WrappedTcpClient, System.Threading.Actors.Services, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", false);
            Assert.IsNotNull(t, "Can't resolve type for WrappedTcpClient");
            Assert.IsTrue(t.IsAssignableFrom(client.GetType()), "Expected ARTC.CreateTcpClientFunc to return WrappedTcpClient");
            Assert.IsTrue(client.Port == -2, "Expected ARTC.CreateTcpClientFunc call 2 to complete via reflection");
            client.Dispose();

            // The initial call should trigger the WrappedTcpClient static constructor, which will replace
            // the default reflection-based creator with an explicit WrappedTcpClient creator.  This
            // will result in a port value of -1.
            client = LightupITcpClientFactory.CreateTcpClientFunc(null, -1); // Special non-connecting constructor
            Assert.IsNotNull(client, "Expected ARTC.CreateTcpClientFunc call 2 to return non-null value");
            t = Type.GetType("System.Threading.Actors.Utilities.WrappedTcpClient, System.Threading.Actors.Services, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", false);
            Assert.IsNotNull(t, "Can't resolve type for WrappedTcpClient");
            Assert.IsTrue(t.IsAssignableFrom(client.GetType()), "Expected ARTC.CreateTcpClientFunc to return WrappedTcpClient");
            Assert.IsTrue(client.Port == -1, "Expected ARTC.CreateTcpClientFunc call 2 to complete via explicit method");
            client.Dispose();
        }
    }
}
